package com.msconfig.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class RoundRectProgressView extends View {
    /**进度条最大值*/
    private float maxCount;
    /**进度条当前值*/
    private float currentCount;
    /**画笔*/
    private Paint mPaint;

    private int mWidth,mHeight;
    private RectF totalRectf;
    private RectF currentRecf;
    private int round;


    public RoundRectProgressView(Context context) {
        this(context,null);
    }

    public RoundRectProgressView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RoundRectProgressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mPaint = new Paint();
        //设置抗锯齿效果
        mPaint.setAntiAlias(true);
        //设置画笔颜色
        round = dipToPx(30);
        totalRectf = new RectF();
        currentRecf = new RectF();
    }

    /***
     * 设置最大的进度值
     * @param maxCount
     */
    public void setMaxCount(float maxCount) {
        this.maxCount = maxCount;
    }
    /**
     * 得到最大进度值
     */
    public double getMaxCount(){
        return maxCount;
    }
    /***
     * 设置当前的进度值
     * @param currentCount
     */
    public void setProgress(float currentCount) {
        this.currentCount = currentCount > maxCount ? maxCount : currentCount;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //画了两个圆角矩形，第一个是最下面的灰色的，第二个是随进度改变的
        super.onDraw(canvas);
        //画总矩形
        mPaint.setColor(0xffe6e6e6);
        totalRectf.left=0;
        totalRectf.top=0;
        totalRectf.right=mWidth;
        totalRectf.bottom=mHeight;
        canvas.drawRoundRect(totalRectf, round, round, mPaint);

        //设置进度条进度矩形
        float section= currentCount/maxCount;
        currentRecf.left=0;
        currentRecf.top=0;
        currentRecf.right=(mWidth)*section;
        currentRecf.bottom=mHeight;
        mPaint.setColor(0xff519af8);
        canvas.drawRoundRect(currentRecf, round, round, mPaint);

    }
    //dip * scale + 0.5f * (dip >= 0 ? 1 : -1)
    private int dipToPx(int dip){
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));//加0.5是为了四舍五入
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        //MeasureSpec.EXACTLY，精确尺寸
        if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
            mWidth = widthSpecSize;
        } else {
            mWidth = 0;
        }
        //MeasureSpec.AT_MOST，最大尺寸，只要不超过父控件允许的最大尺寸即可，MeasureSpec.UNSPECIFIED未指定尺寸
        if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
            mHeight = dipToPx(20);
        } else {
            mHeight = heightSpecSize;
        }
        //设置控件实际大小
        setMeasuredDimension(mWidth, mHeight);
    }

}
